package com.ruoyi.charge.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.charge.auth.service.LoginTokenService;
import com.ruoyi.common.utils.ServletUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.charge.vo.AgentRVo;
import com.ruoyi.charge.bo.AgentRQueryBo;
import com.ruoyi.charge.bo.AgentRAddBo;
import com.ruoyi.charge.bo.AgentREditBo;
import com.ruoyi.charge.service.IAgentRService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 代理关系Controller
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Api(value = "代理关系控制器", tags = {"代理关系管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/charge/agnet_r")
public class AgentRController extends BaseController {

    private final IAgentRService iAgentRService;
    private final LoginTokenService loginTokenService;

    /**
     * 查询代理关系列表
     */
    @ApiOperation("查询代理关系列表")
    @GetMapping("/list")
    public TableDataInfo<AgentRVo> list(AgentRQueryBo bo) {
        Long userId = loginTokenService.getLoginUserId(ServletUtils.getRequest());
        bo.setAgentId(userId);
        startPage();
        List<AgentRVo> list = iAgentRService.queryList(bo);
        return getDataTable(list);
    }


    /**
     * 获取代理关系详细信息
     */
    @ApiOperation("获取代理关系详细信息")
    @GetMapping("/{id}")
    public AjaxResult<AgentRVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iAgentRService.queryById(id));
    }

    /**
     * 新增代理关系
     */
    @ApiOperation("新增代理关系")
    @Log(title = "代理关系", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody AgentRAddBo bo) {
        Long userId = loginTokenService.getLoginUserId(ServletUtils.getRequest());
        bo.setAgentId(userId);
        return toAjax(iAgentRService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改代理关系
     */
    @ApiOperation("修改代理关系")
    @Log(title = "代理关系", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody AgentREditBo bo) {
        return toAjax(iAgentRService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除代理关系
     */
    @ApiOperation("删除代理关系")
    @Log(title = "代理关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iAgentRService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
